/*
 * Event modules
 * author : DUCDQ
 */

 $(function(){
 	//initialized calendar
	$("#departure_date").datepicker({showOn: 'button', buttonImage: '/public/library/jquery/calendar/calendar.gif', buttonImageOnly: true,numberOfMonths: 2,showButtonPanel: true, calendarType: 'solar'});
	$("#departure_date").change(function()
		{
			if ($("#return_date").val().length == 0)
				  $("#return_date").val($(this).val()); //set finishDate to be the same as startDate, automatically
		}
	);
	
	$("#return_date").datepicker({showOn: 'button', buttonImage: '/public/library/jquery/calendar/calendar.gif', buttonImageOnly: true,numberOfMonths: 2,showButtonPanel: true, calendarType: 'solar'});
   	$.datepicker.setDefaults($.extend({showMonthAfterYear: false}, $.datepicker.regional['']));
	$("#departure_date").datepicker($.datepicker.regional['vi']);
    $("#return_date").datepicker($.datepicker.regional['vi']);

	$("#newEvent").submit(function(){
		if($("#frequency").val() != 'weekly' && $("#frequency").val() != 'daily') 
		{
			var f = $("#startDate").val();
			var t = $("#finishDate").val();
			if (f.length <= 0)
			{
				alert("You must chose startDate");
				return false;
			}
			else
			{
				var a = f.split(/[/.-]/);
				var startDate = parseInt(a[0]) + parseInt(a[1]) * 35 + parseInt(a[2]) * 500;
				if (t.length == 0)
				{
					alert("You chose startDate but did not choose finishDate");
					return false;
				}
				else 
				{
					var b = t.split(/[/.-]/);
					var finishDate = parseInt(b[0]) + parseInt(b[1]) * 35 + parseInt(b[2]) * 500;
					if (parseInt(startDate) > parseInt(finishDate))
					{
						alert("startDate must be before finishDate");
						return false;
					}
					else if(parseInt(startDate) == parseInt(finishDate))
					{
						if($("#startHour").val() > $("#finishHour").val())
						{
							alert("startHour must be before finishHour");
							return false;
						}
						else if ($("#startHour").val() == $("#finishHour").val() && $("#startMinute").val() >= $("#finishMinute").val())
						{
							alert("startMinute must be before finishMinute");
							return false;
						}
					}
				}
			}
		}
		else
		{
			var f = 60 * $("#finishHour").val() + $("#finishMinute").val();
			var s = 60 * $("#startHour").val() + $("#startMinute").val();
			//alert(s + '   ' +f);
			if(($("#frequency").val() == 'weekly'  && $("#startDay").val() == $("#finishDay").val()) || $("#frequency").val() == 'daily')
			{  
				//if (parseInt(s) > parseInt(f))
				if (s > f)
				{
					alert("startTime must be before finishTime");
					return false;
				}
			}
		}
     });
	
 	$("#frequency").change(function(e){
 		switch($("#frequency").val())
 		{
 			case 'once' : 
 			case 'monthly': 
 			case 'yearly': 
 			{
 				$("#eventDate").show();
 				$("#eventDay").hide();
 				console.log('monthly or yearly or once');
 				break;
 			}
 			case 'daily': 
 			{
 				$("#eventDate").hide();
 				$("#eventDay").hide();
 				console.log('daily');
	 			break;
 			}
 			case 'weekly': 
 			{
 				$("#eventDate").hide();
 				$("#eventDay").show();
 				console.log('weekly');
	 			break;
 			}
 		}
 	});
 	
 	//effect list.phtml
 	$("input:checkbox[value='checkall']").click (function(){
    	if ($(this).attr("checked") )
    		//check all the things in name=category[]
    		$("input:checkbox[name='category\[\]']").attr("checked","checked");
    });
    
    $("input:checkbox[value='uncheckall']").click (function(){
	if ($(this).attr("checked") )
	{
		//check all the things in name=category[]
		$("input:checkbox[name='category\[\]']").removeAttr("checked");
		$("input:checkbox[value='checkall']").removeAttr("checked");
		$(this).removeAttr("checked");
	}
    });
 	

	//jquery slideUp
	$("fieldset.expansible legend").click(function () {
	      if ($(this).siblings().is(":hidden")) {
	        $(this).siblings().show("slow").end().removeClass("toggle-close").addClass("toggle-open").parent().removeClass("noborder");
	      } else { //close
	        $(this).siblings().slideUp().end().removeClass("toggle-open").addClass("toggle-close").parent().addClass("noborder");
	      }
	    });
	 $("fieldset.expansible legend").addClass("toggle-open");
	
	// changeStaytus 	
 });
 
 //get type and populate type
 //List category and type in file phtml
function getType()
{
 $("select#category_tour").change(function()
	{  
		$.getJSON("/en/tour/Ajax/get-type",
			{
				category: $(this).val(), 
				ajax: 'true'
			}, 
            function(json){
                var options = '';
                for (var i = 0; i < json.length; i++) {
                       options += '<input type="checkbox" value="' + json[i].id + '" name="type[]"/>' + json[i].name +'<br/>';
                }
               	$("#sub-type").html(options);
               	populate_category_checkboxes();
         });
 });
}
